package com.kmvc.message.activity;

import com.kmvc.message.R;
import com.kmvc.message.service.DownloadService;
import com.kmvc.message.tools.NetWorkTool;
import com.kmvc.ui.IphoneTitleBarActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class AboutActivity extends IphoneTitleBarActivity implements OnClickListener{
	
	private TextView upgradeTV,introductionTV,feedbackTV,user_agreement_text,subVersion;
	private String tel = "";
	private String versionName;
	private int localVersion;
	private int netVersion;
	private Handler updatehandler = new Handler();
	
	protected void onCreate(Bundle paramBundle){
	    super.onCreate(paramBundle);
	    setContentView(R.layout.about);
	    setTitle(R.string.about);
	    setLeftButton(R.string.setting, null);
	    
	    upgradeTV = (TextView)findViewById(R.id.upgradeTV);
	    introductionTV = (TextView)findViewById(R.id.introductionTV);
	    feedbackTV = (TextView)findViewById(R.id.feedbackTV);
	    user_agreement_text=(TextView)findViewById(R.id.user_agreement_text);
	    subVersion=(TextView)findViewById(R.id.subVersion);
	    
	    try {
			versionName = getPackageManager().getPackageInfo("com.kmvc.message", 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			versionName="数据读取出错";
		}
	    
	    upgradeTV.setOnClickListener(this);
	    introductionTV.setOnClickListener(this);
	    feedbackTV.setOnClickListener(this);
	    user_agreement_text.setOnClickListener(this);
	  }

	@Override
	public void onClick(View view) {
		switch(view.getId()){
		case R.id.upgradeTV:
			CheckUpdate();
			break;
		case R.id.introductionTV:
			Intent intent2 = new Intent(AboutActivity.this,IntroduceFunctionActivity.class);
			startActivity(intent2);
			break;
		case R.id.feedbackTV:
			Intent intent3=new Intent(AboutActivity.this,MessageFanKui.class);
			startActivity(intent3);
			break;
		case R.id.user_agreement_text:
			Intent inten4 = new Intent(AboutActivity.this,UserAgreementActivity.class);
			startActivity(inten4);
			break;
		}
	}
	
	private void CheckUpdate(){
		new Thread(){
			public void run(){
				String temp="0";
				try {
					temp = NetWorkTool.getContent("http://222.221.20.141/qrcode/MessageAndroid.txt");
					tel = NetWorkTool.getContent("http://222.221.20.141/qrcode/txt/tell.txt");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					temp="0";
				}
				netVersion = Integer.parseInt(temp);
				updatehandler.post(runnableupdate);
			}
		}.start();
		try {
			versionName = getPackageManager().getPackageInfo("com.kmvc.message", 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			versionName="数据读取出错";
		}

	}
	
	Runnable runnableupdate = new Runnable() {
		public void run() {
			try {
				localVersion = getPackageManager().getPackageInfo("com.kmvc.message", 0).versionCode;
				SharedPreferences shp = getSharedPreferences("phonenumber", 0);
				Editor editor = shp.edit();
				editor.putString("tel", tel);
				editor.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (localVersion < netVersion) {
				Intent intent = new Intent();
				intent.setClass(AboutActivity.this, DownloadService.class);
				intent.putExtra("PNAME", "Message.apk");
				intent.putExtra("URL",
						"http://222.221.20.141/qrcode/apk/Message.apk");
				intent.putExtra("NAME", "信息通报");
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				AboutActivity.this.startService(intent);
			}else{
				Toast.makeText(AboutActivity.this, "您的当前软件已是最新版!", Toast.LENGTH_LONG).show();
			}
		}
	};
	
	@Override
	public void onResume(){
		super.onResume();
		subVersion.setText("软件版本:"+versionName);
	}
}
